package com.wp.redisSpringBoot01.service.impl;

import com.wp.redisSpringBoot01.dao.DeptDao;
import com.wp.redisSpringBoot01.entity.Dept;
import com.wp.redisSpringBoot01.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author: WangPeng
 * @createTime: 2022/12/07 13:24
 * @description:
 * @version: v1.0
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptDao deptDao;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    @Override
    //cacheNames:缓存的的名称    key: 唯一区分 #id读取方法的参数名的值
    @Cacheable(cacheNames= "redis::dept",key = "#id")
    public Dept findById(int id) {
        Dept dept = deptDao.selectById(id);
        return dept;
    }

    @Override
    @CacheEvict(cacheNames = "redis::dept",key = "#id")
    public Integer delete(Integer id) {
        int i = deptDao.deleteById(id);
        return i;
    }

    @Override
    public Dept save(Dept dept) {
        int i = deptDao.insert(dept);
        return dept;
    }

    @Override
    @CachePut(cacheNames = "redis::dept",key = "#dept")
    public Dept update(Dept dept) {
        //先修改数据库。
        int i = deptDao.updateById(dept);
        return dept;
    }
}
